package com.tianbing.adlottery.controller.user;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.fengniu.ejb.business.IContentBusiness;
import com.fengniu.ejb.business.ICouponBusiness;
import com.fengniu.ejb.business.ISiteBusiness;
import com.fengniu.ejb.business.IUserBusiness;
import com.fengniu.ejb.dao.ICategoryDao;
import com.fengniu.ejb.dao.IContentDao;
import com.fengniu.ejb.dao.ISiteDao;
import com.fengniu.ejb.dao.ISystemDao;
import com.fengniu.ejb.dao.IUserDao;
import com.fengniu.ejb.entity.CouponItem;
import com.fengniu.ejb.entity.Site2Content;
import com.fengniu.ejb.entity.User2Content;
import com.fengniu.ejb.entity.User2Site;
import com.tianbing.adlottery.helper.SkinUtils;
import com.tianbing.adlottery.util.UserSession;
import com.tianbing.adlottery.util.UtilDataGroup;

/**
 * MultiActionController for the image list/upload UI.
 * 
 * @author Conan Chen
 * @since 2006.12.15
 */
public class MySpaceController extends MultiActionController {
	Logger logger = Logger.getLogger(MySpaceController.class.getName());

	private String mySpaceView;

	private String listMyMessageView;

	private String listMyCouponItemView;

	private String listMyFloatBookView;

	private String listMyFavoriteWebSiteView;

	private String myHomeHordeView;

	private String category2HordeListView;

	private String setMyHomeHordeSuccessView;

	private String addFavoriteProductSuccessView;

	private String addFavoriteStoreSuccessView;

	private String addFavoriteHordeSuccessView;

	private String addFavoriteWebSiteSuccessView;

	private String addFavoriteCompanySuccessView;

	private String deleteFavoriteProductSuccessView;

	private String deleteFavoriteStoreSuccessView;

	private String deleteFavoriteWebSiteSuccessView;

	private String deleteFavoriteCompanySuccessView;

	private String ajaxAutoLoginSuccView;

	private IUserBusiness userBusiness;

	private ISiteBusiness siteBusiness;

	private IContentBusiness contentBusiness;

	private ICouponBusiness couponBusiness;

	private ISiteDao siteDao;

	private IUserDao userDao;

	private IContentDao contentDao;

	private ICategoryDao categoryDao;

	private SkinUtils skinUtils;

	private ISystemDao systemDao;

	public ModelAndView showMySpace(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelAndView mv = new ModelAndView(mySpaceView);
		Map<String, Object> model = mv.getModel();
		UserSession auth = skinUtils.getUserAuthorization(request, response);

		Map couponItems = couponBusiness.getCouponItems(auth.getUserSysId(), -1, -1, -1, 0, 10);
		List couponItemList = (List) couponItems.get("couponItemList");
		List couponItemGroupList = UtilDataGroup.makeGroups(couponItemList, 0, 6);
		model.put("couponItemGroupList", couponItemGroupList);
		// ---------------
		List<User2Content> user2ContentList = userBusiness.getListUserFavoriteProduct(auth.getUserSysId(), -1, 0, 20);
		List user2ContentGroupList = UtilDataGroup.makeGroups(user2ContentList, 0, 6);
		model.put("user2ContentGroupList", user2ContentGroupList);
		// ---------------
		List<User2Site> user2StoreList = userBusiness.getListUserFavoriteStore(auth.getUserSysId(), -1, 0, 20);
		List user2StoreGroupList = UtilDataGroup.makeGroups(user2StoreList, 0, 6);
		model.put("user2StoreGroupList", user2StoreGroupList);
		return mv;
	}

	public ModelAndView listMyMessage(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelAndView mv = new ModelAndView(listMyMessageView);
		Map<String, Object> model = mv.getModel();
		UserSession auth = skinUtils.getUserAuthorization(request, response);

		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		Integer pgsize = 30;// ServletRequestUtils.getIntParameter(request,
		// "pgsize", 30);

		List<User2Content> user2ContentList = userBusiness.getListUserPostMessage(auth.getUserSysId(), pgno, pgsize);
		List user2ContentGroupList = UtilDataGroup.makeGroups(user2ContentList, 0, 4);

		Long totalCount = 100l;// contentDao.getCountContent2CategoryBy(null,
		// relationIds, cgid, brdgid, null, null);

		model.put("user2ContentGroupList", user2ContentGroupList);

		model = skinUtils.preparePageFooter(request, user2ContentList, totalCount, pgno, pgsize, model);

		return mv;
	}

	public ModelAndView listMyFavoriteWebSite(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		ModelAndView mv = new ModelAndView(listMyFavoriteWebSiteView);
		Map<String, Object> model = mv.getModel();
		UserSession auth = skinUtils.getUserAuthorization(request, response);

		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		Integer pgsize = 30;// ServletRequestUtils.getIntParameter(request,
		// "pgsize", 30);

		List<User2Site> user2SiteList = userBusiness.getListUserFavoriteWebSite(auth.getUserSysId(), -1, pgno, pgsize);
		List user2SiteGroupList = UtilDataGroup.makeGroups(user2SiteList, 0, 4);

		Long totalCount = 100l;// contentDao.getCountContent2CategoryBy(null,
		// relationIds, cgid, brdgid, null, null);

		model.put("user2SiteGroupList", user2SiteGroupList);

		model = skinUtils.preparePageFooter(request, user2SiteList, totalCount, pgno, pgsize, model);

		return mv;
	}

	public ModelAndView listMyFloatBook(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelAndView mv = new ModelAndView(listMyFloatBookView);
		Map<String, Object> model = mv.getModel();
		UserSession auth = skinUtils.getUserAuthorization(request, response);

		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		Integer pgsize = 30;// ServletRequestUtils.getIntParameter(request,
		// "pgsize", 30);

		List<User2Content> user2ContentList = userBusiness.getListUserFloatBook(auth.getUserSysId(), -1, pgno, pgsize);
		List user2ContentGroupList = UtilDataGroup.makeGroups(user2ContentList, 0, 4);

		Long totalCount = 100l;// contentDao.getCountContent2CategoryBy(null,
		// relationIds, cgid, brdgid, null, null);

		model.put("user2ContentGroupList", user2ContentGroupList);

		model = skinUtils.preparePageFooter(request, user2ContentList, totalCount, pgno, pgsize, model);

		return mv;
	}

	public ModelAndView listMyCouponItem(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelAndView mv = new ModelAndView(listMyCouponItemView);
		Map<String, Object> model = mv.getModel();
		UserSession auth = skinUtils.getUserAuthorization(request, response);

		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		Integer pgsize = 30;// ServletRequestUtils.getIntParameter(request,
		// "pgsize", 30);

		Map storeCouponItems = couponBusiness.getCouponItems(auth.getUserSysId(), -1, -1, -1, pgno, pgsize);
		List<CouponItem> couponItemList = (List<CouponItem>) storeCouponItems.get("couponItemList");
		Long totalCount = (Long) storeCouponItems.get("totalCount");

		if (couponItemList != null) {
			for (CouponItem ci : couponItemList) {
				if (ci.getCoupon() != null) {
					Site2Content s2c = couponBusiness.findCouponStoreByCouponItemSysId(ci.getSysId());

					if (s2c != null) {
						ci.setSite(s2c.getSite());
					}
				}
			}
		}

		model.put("couponItemList", couponItemList);
		model.put("totalCount", totalCount);

		model = skinUtils.preparePageFooter(request, couponItemList, totalCount, pgno, pgsize, model);

		return mv;
	}

	public ModelAndView viewMyHomeHorde(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelAndView mv = new ModelAndView();
		Map<String, Object> model = mv.getModel();
		UserSession auth = skinUtils.getUserAuthorization(request, response);
		User2Site myHomeHorde = userBusiness.findUserHomeHorde(auth.getUserSysId());
		if (myHomeHorde != null) {
			mv.setViewName(myHomeHordeView + "?ssid=" + myHomeHorde.getSite().getSysId());
		} else {
			mv.setViewName(category2HordeListView);
		}

		return mv;
	}

	public ModelAndView addFavoriteProduct(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(addFavoriteProductSuccessView);
		Map<String, Object> model = mv.getModel();

		UserSession auth = skinUtils.getUserAuthorization(request, response);
		long csid = ServletRequestUtils.getLongParameter(request, "csid", -1);
		if (csid > 0) {
			userBusiness.addUserFavoriteProduct(auth.getUserSysId(), csid);
		}

		return mv;
	}

	public ModelAndView setMyHomeHorde(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(setMyHomeHordeSuccessView);
		Map<String, Object> model = mv.getModel();
		UserSession auth = skinUtils.getUserAuthorization(request, response);
		long ssid = ServletRequestUtils.getLongParameter(request, "ssid", -1);
		if (ssid > 0) {
			userBusiness.setUserHomeHorde(auth.getUserSysId(), ssid);
			skinUtils.refreshUserAuthorization(request, response);
		}

		return mv;
	}

	public ModelAndView addFavoriteStore(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(addFavoriteStoreSuccessView);
		Map<String, Object> model = mv.getModel();
		UserSession auth = skinUtils.getUserAuthorization(request, response);
		long ssid = ServletRequestUtils.getLongParameter(request, "ssid", -1);
		if (ssid > 0) {
			userBusiness.addUserFavoriteStore(auth.getUserSysId(), ssid);
		}

		return mv;
	}

	public ModelAndView addFavoriteHorde(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(addFavoriteHordeSuccessView);
		Map<String, Object> model = mv.getModel();
		UserSession auth = skinUtils.getUserAuthorization(request, response);
		long ssid = ServletRequestUtils.getLongParameter(request, "ssid", -1);
		if (ssid > 0) {
			userBusiness.addUserFavoriteHorde(auth.getUserSysId(), ssid);
		}

		return mv;
	}

	public ModelAndView addFavoriteWebSite(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(addFavoriteWebSiteSuccessView);
		Map<String, Object> model = mv.getModel();
		UserSession auth = skinUtils.getUserAuthorization(request, response);
		long ssid = ServletRequestUtils.getLongParameter(request, "ssid", -1);
		if (ssid > 0) {
			userBusiness.addUserFavoriteWebSite(auth.getUserSysId(), ssid);
		}

		return mv;
	}

	public ModelAndView addFavoriteCompany(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(addFavoriteCompanySuccessView);
		Map<String, Object> model = mv.getModel();

		UserSession auth = skinUtils.getUserAuthorization(request, response);
		long ssid = ServletRequestUtils.getLongParameter(request, "ssid", -1);
		if (ssid > 0) {
			userBusiness.addUserFavoriteCompany(auth.getUserSysId(), ssid);
		}
		return mv;
	}

	public ModelAndView deleteFavoriteProduct(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView mv = new ModelAndView(deleteFavoriteProductSuccessView);
		Map<String, Object> model = mv.getModel();

		UserSession auth = skinUtils.getUserAuthorization(request, response);
		long csid = ServletRequestUtils.getLongParameter(request, "csid", -1);
		if (csid > 0) {
			userBusiness.deleteUserFavoriteProduct(auth.getUserSysId(), csid);
		}
		return mv;
	}

	public ModelAndView deleteFavoriteStore(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(deleteFavoriteStoreSuccessView);
		Map<String, Object> model = mv.getModel();
		UserSession auth = skinUtils.getUserAuthorization(request, response);
		long ssid = ServletRequestUtils.getLongParameter(request, "ssid", -1);
		if (ssid > 0) {
			userBusiness.deleteUserFavoriteStore(auth.getUserSysId(), ssid);
		}

		return mv;
	}

	public ModelAndView deleteFavoriteWebSite(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView mv = new ModelAndView(deleteFavoriteWebSiteSuccessView);
		Map<String, Object> model = mv.getModel();
		UserSession auth = skinUtils.getUserAuthorization(request, response);
		long ssid = ServletRequestUtils.getLongParameter(request, "ssid", -1);
		if (ssid > 0) {
			userBusiness.deleteUserFavoriteWebSite(auth.getUserSysId(), ssid);
		}

		return mv;
	}

	public ModelAndView deleteFavoriteCompany(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView mv = new ModelAndView(deleteFavoriteCompanySuccessView);
		Map<String, Object> model = mv.getModel();
		UserSession auth = skinUtils.getUserAuthorization(request, response);
		long ssid = ServletRequestUtils.getLongParameter(request, "ssid", -1);
		if (ssid > 0) {
			userBusiness.deleteUserFavoriteCompany(auth.getUserSysId(), ssid);
		}

		return mv;
	}

	public ModelAndView ajaxAutoLogin(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(ajaxAutoLoginSuccView);
		Map<String, Object> model = mv.getModel();
		UserSession auth = skinUtils.getUserAuthorization(request, response);
		skinUtils.getLastVisitedHorde(request, response);
		skinUtils.setHasCheckedAutoLogin(request, response);
		return mv;
	}

	public void setCategoryDao(ICategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}

	public void setMySpaceView(String homeView) {
		this.mySpaceView = homeView;
	}

	public void setContentDao(IContentDao contentDao) {
		this.contentDao = contentDao;
	}

	public void setLogger(Logger logger) {
		this.logger = logger;
	}

	public void setSiteDao(ISiteDao siteDao) {
		this.siteDao = siteDao;
	}

	public void setSkinUtils(SkinUtils skinUtils) {
		this.skinUtils = skinUtils;
	}

	public void setSystemDao(ISystemDao systemDao) {
		this.systemDao = systemDao;
	}

	public void setUserBusiness(IUserBusiness userBusiness) {
		this.userBusiness = userBusiness;
	}

	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	public void setAddFavoriteCompanySuccessView(String addCompany2MySpaceSuccessView) {
		this.addFavoriteCompanySuccessView = addCompany2MySpaceSuccessView;
	}

	public void setAddFavoriteProductSuccessView(String addProduct2MySpaceSuccessView) {
		this.addFavoriteProductSuccessView = addProduct2MySpaceSuccessView;
	}

	public void setAddFavoriteStoreSuccessView(String addStore2MySpaceSuccessView) {
		this.addFavoriteStoreSuccessView = addStore2MySpaceSuccessView;
	}

	public void setDeleteFavoriteCompanySuccessView(String deleteFavoriteCompanySuccessView) {
		this.deleteFavoriteCompanySuccessView = deleteFavoriteCompanySuccessView;
	}

	public void setDeleteFavoriteProductSuccessView(String deleteFavoriteProductSuccessView) {
		this.deleteFavoriteProductSuccessView = deleteFavoriteProductSuccessView;
	}

	public void setDeleteFavoriteStoreSuccessView(String deleteFavoriteStoreSuccessView) {
		this.deleteFavoriteStoreSuccessView = deleteFavoriteStoreSuccessView;
	}

	public void setListMyMessageView(String listMyMessageView) {
		this.listMyMessageView = listMyMessageView;
	}

	public void setSetMyHomeHordeSuccessView(String setMyHomeHordeSuccessView) {
		this.setMyHomeHordeSuccessView = setMyHomeHordeSuccessView;
	}

	public void setAddFavoriteWebSiteSuccessView(String addFavoriteWebSiteSuccessView) {
		this.addFavoriteWebSiteSuccessView = addFavoriteWebSiteSuccessView;
	}

	public void setAddFavoriteHordeSuccessView(String addFavoriteHordeSuccessView) {
		this.addFavoriteHordeSuccessView = addFavoriteHordeSuccessView;
	}

	public void setListMyFloatBookView(String listMyFloatBookView) {
		this.listMyFloatBookView = listMyFloatBookView;
	}

	public void setListMyCouponItemView(String listMyCouponItemView) {
		this.listMyCouponItemView = listMyCouponItemView;
	}

	public void setListMyFavoriteWebSiteView(String listMyFavoriteWebSiteView) {
		this.listMyFavoriteWebSiteView = listMyFavoriteWebSiteView;
	}

	public void setDeleteFavoriteWebSiteSuccessView(String deleteFavoriteWebSiteSuccessView) {
		this.deleteFavoriteWebSiteSuccessView = deleteFavoriteWebSiteSuccessView;
	}

	public void setAjaxAutoLoginSuccView(String ajaxAutoLoginSuccView) {
		this.ajaxAutoLoginSuccView = ajaxAutoLoginSuccView;
	}

	public void setMyHomeHordeView(String myHomeHordeView) {
		this.myHomeHordeView = myHomeHordeView;
	}

	public void setCategory2HordeListView(String category2HordeListView) {
		this.category2HordeListView = category2HordeListView;
	}

	public void setCouponBusiness(ICouponBusiness couponBusiness) {
		this.couponBusiness = couponBusiness;
	}

	public void setSiteBusiness(ISiteBusiness siteBusiness) {
		this.siteBusiness = siteBusiness;
	}

	public void setContentBusiness(IContentBusiness contentBusiness) {
		this.contentBusiness = contentBusiness;
	}

}
